package com.property.business.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvcConfig
 *
 * @author 西瓜
 * @date 2023年11月30日 11:28
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${files.upload.path}")
    private String filePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/filePreview/**").addResourceLocations("file:" + filePath);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
                .allowedHeaders("Authentication,Origin, X-Requested-With, Content-Type, " + "Accept, x-access-token")
                .exposedHeaders("header1", "header2")
                .allowCredentials(false)
                .maxAge(3600)
        ;
    }
}